package com.rays.gank.ui.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.rays.gank.R
import com.rays.gank.databinding.MainActivityBinding
import com.rays.gank.delegate.viewBinding
import com.rays.gank.ext.logD
import com.rays.gank.ui.fragment.GanHuoFragment
import com.rays.gank.ui.fragment.HomeFragment
import com.rays.gank.ui.fragment.MineFragment
import com.rays.gank.ui.fragment.MovieFragment

/**
 * Created by Rays on 2021/1/21.
 */
class MainActivity : BaseActivity() {
    private val activityBinding: MainActivityBinding by viewBinding()

    override fun getLayoutResId() = R.layout.main_activity

    override fun initView(savedInstanceState: Bundle?) {
        with(activityBinding.mainViewpager) {
            isUserInputEnabled = false
            offscreenPageLimit = 4
            adapter = object : FragmentStateAdapter(this@MainActivity) {
                override fun getItemCount() = 4

                override fun createFragment(position: Int): Fragment {
                    "createFragment $position".logD("MainActivity")
                    return when (position) {
                        0 -> HomeFragment()
                        1 -> GanHuoFragment()
                        2 -> MovieFragment()
                        3 -> MineFragment()
                        else -> HomeFragment()
                    }
                }
            }
        }

        with(activityBinding.mainBottom) {
            enableAnimation(true)
            setOnNavigationItemSelectedListener {
                "setOnNavigationItemSelectedListener $it".logD("MainActivity")
                when (it.itemId) {
                    R.id.menu_main -> activityBinding.mainViewpager.setCurrentItem(0, false)
                    R.id.menu_gan_huo -> activityBinding.mainViewpager.setCurrentItem(1, false)
                    R.id.menu_movie -> activityBinding.mainViewpager.setCurrentItem(2, false)
                    R.id.menu_mine -> activityBinding.mainViewpager.setCurrentItem(3, false)
                }
                true
            }
        }
    }
}